$(document).ready( function() {
    var getTimestampFromDateTime = function(dateTime) {
        var dateStamp = "";
        
        if(dateTime) {
            var date = dateTime.split(" ")[0];
            var time = dateTime.split(" ")[1];

            dateParts = date.split(".");
            timeParts = time.split(":");

            var jsDate = dateParts[1] + "/" + dateParts[0] + "/" + dateParts[2];
            dateStamp = new Date(jsDate).getTime() / 1000;
            dateStamp += (timeParts[0] * 60 * 60) + (timeParts[1] * 60) + (60 * 60 * 3);   
        }

        return dateStamp;
    };
    
    var getDateTimeFromTimestamp = function(timeStamp) {
        if(timeStamp) {
            var dateTime = new Date((timeStamp - (60 * 60 * 3))*1000);

            var month = dateTime.getMonth();
            var day = dateTime.getDate();
            var year = dateTime.getFullYear();

            var hour = dateTime.getHours();
            var minutes = dateTime.getMinutes();

            return (day < 10 ? "0" : "") + day + "..." + (month < 10 ? "0" : "") + (month+1) + "." + year + " " + (hour < 10 ? "0" : "") + hour + ":" + (minutes < 10 ? "0" : "") + minutes;
        }  

        return 0;
    };
    
    $("#Movie_Title").autocomplete({
        source: function (request, response) {
            jQuery.get("/api/titles", {
                term: request.term
            }, function (data) {
                var JSONObj = $.parseJSON(data);
                response(JSONObj);
            });
        },
        response: function(event, ui) {
            if(ui.content.length < 1) {
                $(this).removeClass("loading").css("background-color", "OrangeRed");
            }
        },
        search: function(event, info) {
            $(this).css("background-color", "white").addClass("loading");
        },
        open: function(event, info) {
            $(this).removeClass("loading");
        },
        select: function(event, info) {
            $("#Movie_ID")[0].value = info.item.value;
            info.item.value = info.item.label;
            $("#Movie_duration")[0].innerText = (Math.floor(info.item.duration / 60)) + ' stundas ' + (info.item.duration % 60) + ' minūtes';
            $("#Movie_duration_mins")[0].value = (info.item.duration * 60);
            $(this).removeClass("loading");
        },
        minLength: 2
    });  
    
    $("#Youtube_Trailer").click(function() {
        $.fancybox({
            'padding'		: 0,
            'autoScale'		: false,
            'transitionIn'	: 'none',
            'transitionOut'	: 'none',
            'title'			: this.title,
            'width'		: 680,
            'height'		: 495,
            'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
            'type'			: 'swf',
            'swf'			: {
            'wmode'		: 'transparent',
            'allowfullscreen'	: 'true'
            }
        });

        return false;
    });
});